/**     
 * @Title: UserController.java
 * @Package com.zhfd.server.web
 * @Description: TODO
 * @author weiwei 
 * @date 2017/10/15
 * @version V1.0     
 */
package com.xnkf.server.web;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xnkf.common.spring.annotation.ReqJson;
import com.xnkf.common.util.Constants;
import com.xnkf.common.util.RetResult;
import com.xnkf.common.util.RspUtils;
import com.xnkf.common.util.SessionUtils;
import com.xnkf.server.service.UserService;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author weiwei
 * @date 2017/10/15
 */
@RestController
@RequestMapping("/v1/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	/**
	 * @Title: login
	 * @Description: 登录
	 * @param appVersion
	 * @param os
	 * @param phone
	 * @param passwd
	 * @return Object
	 */
	@RequestMapping(value = "login.do", method = RequestMethod.POST, produces = Constants.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object login(@ReqJson String name, @ReqJson String passwd) {
		RetResult retResult = userService.login(name, passwd);
		Object obj = retResult.getData(Constants.PROPERTY_COMMON_DATA_KEY);
		return RspUtils.makeRsp(retResult, obj == null ? new Object() : obj);
	}

	/**
	 * @Title: logout
	 * @Description: 登出
	 * @param userId
	 * @return Object
	 */
	@RequestMapping(value = "logout.do", method = RequestMethod.POST, produces = Constants.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object logout(@ReqJson String userId) {
		RetResult result = checkUser(userId);
		if (null != result) {
			return RspUtils.makeRsp(result);
		}
		Object rs = SessionUtils.removeAuthcToken();
		SecurityUtils.getSubject().logout();
		if (rs != null) {
			return RspUtils.makeOkRsp();
		}
		return RspUtils.makeRsp("登出失败");
	}

	/**
	 * @Title: registration
	 * @Description: 注册
	 * @param name
	 * @param age
	 * @param passwd
	 * @return Object
	 */
	@RequestMapping(value = "reg.do", method = RequestMethod.POST, produces = Constants.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object registration(@ReqJson String name, @ReqJson Integer age, @ReqJson String passwd) {
		RetResult retResult = userService.registration(name, age, passwd);
		return RspUtils.makeRsp(retResult);
	}

	/**
	 * @Title: queryName
	 * @Description: TODO
	 * @param name
	 * @return Object
	 */
	@RequestMapping(value = "queryName.do", method = RequestMethod.POST, produces = Constants.CONTENT_TYPE_APPLICATION_JSON)
	@ResponseBody
	public Object queryName(@ReqJson String userId, @ReqJson String name) {
		RetResult result = checkUser(userId);
		if (null != result) {
			return RspUtils.makeRsp(result);
		}
		RetResult retResult = userService.queryName(name);
		Object obj = retResult.getData(Constants.PROPERTY_COMMON_DATA_KEY);
		return RspUtils.makeRsp(retResult, obj == null ? new Object() : obj);
	}
}